package com.nnxieli.trans.ui.activity;

import android.Manifest;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hd.trans.TranslationUI;
import com.hd.trans.network.bean.UserInfoBean;
import com.hd.trans.ui.activity.AiChatActivity;
import com.hd.trans.ui.activity.CameraHomeActivity;
import com.hd.trans.ui.activity.FavoritesActivity;
import com.hd.trans.ui.activity.FilesHomeActivity;
import com.hd.trans.ui.activity.OfflineHomeActivity;
import com.hd.trans.ui.activity.SimulHomeActivity;
import com.hd.trans.ui.activity.SimulHomeActivity2;
import com.hd.trans.ui.activity.TextHistoryActivity;
import com.hd.trans.ui.activity.TextHomeActivity;
import com.hd.trans.ui.activity.VoiceFirstAidKitActivity;
import com.hd.trans.ui.activity.VoiceHomeActivity;
import com.hd.trans.ui.activity.VoiceHomeTwoActivity;
import com.hd.trans.ui.activity.VoicePlaySettingActivity;
import com.hd.trans.ui.activity.WebHomeActivity;
import com.hd.trans.ui.activity.WordTranslateActivity;
import com.hd.trans.ui.activity.customize.PictureTranslateActivity;
import com.hd.trans.ui.activity.customize.SimulTranslateActivity;
import com.hd.trans.ui.activity.customize.TextTranslateActivity;
import com.hd.trans.ui.activity.customize.VoiceTranslateActivity;
import com.hd.trans.ui.base.BaseActivity;
import com.hd.trans.utils.PermissionCallback;
import com.hd.trans.widgets.views.floatwindow.apply.OnFloatClickListener;
import com.hd.trans.widgets.views.floatwindow.apply.SimultaneousFloatWindow;
import com.hd.trans.widgets.views.floatwindow.component.ShowType;
import com.nnxieli.trans.R;
import com.nnxieli.trans.adapter.HomeAdapter;
import com.nnxieli.trans.bean.HomeIconBean;
import com.nnxieli.trans.util.LimitUserImpl;

import java.util.ArrayList;

public class MainActivity extends BaseActivity {

    private RecyclerView mRecycler;
    private ArrayList<HomeIconBean> iconList;
    private LinearLayout filesButton;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initViewsAndEvents(Bundle savedInstanceState) {
//        TransInit.Builder(this).setThemeColor("#725e82").build();
        initIcon();
        initView();
    }


    private void initIcon() {
        iconList = new ArrayList();
        iconList.add(new HomeIconBean(R.mipmap.icon_home_module_text, getResources().getString(R.string.text_translate), "#0A6BFC", TextHomeActivity.class));
        iconList.add(new HomeIconBean(R.mipmap.icon_home_module_text, "(新)"+getResources().getString(R.string.text_translate), "#845a33", TextTranslateActivity.class));
        iconList.add(new HomeIconBean(R.mipmap.icon_home_module_voice, getResources().getString(R.string.voice_translate), "#404DDF", VoiceHomeActivity.class));
        iconList.add(new HomeIconBean(R.mipmap.icon_home_module_voice, "(新)"+getResources().getString(R.string.voice_translate), "#845a33", VoiceTranslateActivity.class));
        iconList.add(new HomeIconBean(R.mipmap.icon_home_module_simultaneous, getResources().getString(R.string.simultaneous_translate), "#7572FF", SimulHomeActivity.class));
        iconList.add(new HomeIconBean(R.mipmap.icon_home_module_simultaneous, "(新)"+getResources().getString(R.string.simultaneous_translate), "#845a33", SimulTranslateActivity.class));
        iconList.add(new HomeIconBean(R.mipmap.icon_home_module_photograph, getResources().getString(R.string.photograph_translate), "#0050FC", CameraHomeActivity.class));
        iconList.add(new HomeIconBean(R.mipmap.icon_home_module_photograph, "(新)"+getResources().getString(R.string.photograph_translate), "#845a33", PictureTranslateActivity.class));
        iconList.add(new HomeIconBean(R.mipmap.ic_launcher, getResources().getString(R.string.voice_first_aid_kit_str), "#7572FF", VoiceFirstAidKitActivity.class));
        iconList.add(new HomeIconBean(R.mipmap.ic_launcher, getResources().getString(R.string.favorites), "#404DDF", FavoritesActivity.class));
        iconList.add(new HomeIconBean(R.mipmap.ic_launcher, getResources().getString(R.string.web_trans), "#0A6BFC", WebHomeActivity.class));
        iconList.add(new HomeIconBean(R.mipmap.ic_launcher, getResources().getString(R.string.word_translate), "#0A6BFC", WordTranslateActivity.class));
        iconList.add(new HomeIconBean(R.mipmap.ic_launcher, getResources().getString(R.string.voice_setting), "#404DDF", VoicePlaySettingActivity.class));
        iconList.add(new HomeIconBean(R.mipmap.ic_launcher, getResources().getString(R.string.history_text), "#0050FC", TextHistoryActivity.class));
        iconList.add(new HomeIconBean(R.mipmap.ic_launcher, getResources().getString(R.string.ai_translate), "#CC0000", AiChatActivity.class));
        iconList.add(new HomeIconBean(R.mipmap.ic_launcher, getResources().getString(R.string.offline_translate), "#0A6BFC", OfflineHomeActivity.class));
        iconList.add(new HomeIconBean(R.mipmap.icon_home_module_document, getResources().getString(R.string.document_translate), "#CC0000", FilesHomeActivity.class));
        iconList.add(new HomeIconBean(R.mipmap.icon_home_module_video, getResources().getString(R.string.video_translate), "#CC0000", FilesHomeActivity.class));
        iconList.add(new HomeIconBean(R.mipmap.icon_home_module_audio, getResources().getString(R.string.audio_translate), "#CC0000", FilesHomeActivity.class));
        iconList.add(new HomeIconBean(R.mipmap.ic_launcher, getResources().getString(R.string.float_window), "#AA6BFC", null));
    }

    private void initView() {
        filesButton = (LinearLayout) findViewById(R.id.home_file);

        mRecycler = (RecyclerView) findViewById(R.id.home_recycler);
        mRecycler.setLayoutManager(new GridLayoutManager(this, 2));
        HomeAdapter adapter = new HomeAdapter(iconList);
        adapter.setOnItemClickListener(this::start);
        mRecycler.setAdapter(adapter);
    }

    private void start(int position, String name, Class<BaseActivity> clazz) {
        if(clazz != null){
            if (clazz.equals(TextHomeActivity.class)) {
                TextHomeActivity.open(MainActivity.this, LimitUserImpl.getInstance());
            } else if (clazz.equals(TextTranslateActivity.class)) {
                TranslationUI.openTextTransActivity(MainActivity.this);
            }  else if (clazz.equals(SimulHomeActivity.class)) {
                SimulHomeActivity2.open(MainActivity.this, LimitUserImpl.getInstance());
            } else if (clazz.equals(SimulTranslateActivity.class)) {
                TranslationUI.openSimultaneousTransActivity(MainActivity.this);
            } else if (clazz.equals(CameraHomeActivity.class)) {
                String[] permissions = new String[]{
                        Manifest.permission.CAMERA};
                checkPermissionAndStart(permissions, new PermissionCallback() {
                    @Override
                    public void doNext() {
                        CameraHomeActivity.open(MainActivity.this, LimitUserImpl.getInstance(), null);
                    }
                });
            } else if (clazz.equals(PictureTranslateActivity.class)) {
                String[] permissions = new String[]{
                        Manifest.permission.CAMERA};
                checkPermissionAndStart(permissions, new PermissionCallback() {
                    @Override
                    public void doNext() {
                        TranslationUI.openCameraTransActivity(MainActivity.this);
                    }
                });
            } else if (clazz.equals(FilesHomeActivity.class)) {
                if(name.equals(getResources().getString(R.string.document_translate))){
                    TranslationUI.openDocumentTransActivity(MainActivity.this);
                }else if(name.equals(getResources().getString(R.string.video_translate))){
                    TranslationUI.openVideoTransActivity(MainActivity.this);
                }else if(name.equals(getResources().getString(R.string.audio_translate))){
                    TranslationUI.openAudioTransActivity(MainActivity.this);
                }
            } else if (clazz.equals(VoiceHomeActivity.class)) {
                VoiceHomeTwoActivity.open(MainActivity.this, LimitUserImpl.getInstance());
            } else if (clazz.equals(VoiceTranslateActivity.class)) {
                TranslationUI.openVoiceTransActivity(MainActivity.this);
            } else if (clazz.equals(VoiceFirstAidKitActivity.class)) {
                TranslationUI.openVoiceFirstAidKitActivity(MainActivity.this);
            } else if (clazz.equals(FavoritesActivity.class)) {
                TranslationUI.openFavoritesActivity(MainActivity.this);
            } else if (clazz.equals(WebHomeActivity.class)) {
                TranslationUI.openWebTransActivity(MainActivity.this);
            } else if (clazz.equals(WordTranslateActivity.class)) {
                String[] permissions3 = new String[]{
                        Manifest.permission.CAMERA,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE};
                checkPermissionAndStart(permissions3, new PermissionCallback() {
                    @Override
                    public void doNext() {
                        TranslationUI.openWordTransActivity(MainActivity.this);
                    }
                });
            } else if (clazz.equals(VoicePlaySettingActivity.class)) {
                TranslationUI.openVoiceSettingActivity(MainActivity.this, true);
            } else if (clazz.equals(TextHistoryActivity.class)) {
                TranslationUI.openTextHistoryActivity(MainActivity.this);
            } else if (clazz.equals(AiChatActivity.class)) {
                UserInfoBean bean = new UserInfoBean(
                        "DY1gO-KHJljWZUcDVBu78rKVywg52WArHICRZXHQStBXVO9K48p0rh7ip-4V0JtW5pJopEtNLLimBdh44rrTxh3tJQ",
                        "o0j5B5wYZWlE2CXl-REscbRLdYl0",
                        "",
                        ""
                );
                TranslationUI.openAiChatActivity(MainActivity.this, bean);
            } else if (clazz.equals(OfflineHomeActivity.class)) {
                /*String[] permissions5 = new String[]{
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE};
                checkPermissionAndStart(permissions5, new PermissionCallback() {
                    @Override
                    public void doNext() {
                        TranslationUI.openOfflinePkgDownloadActivity(MainActivity.this, null);
                    }
                });*/
                TranslationUI.openOfflinePkgDownloadActivity(MainActivity.this);
            }
        }else {
            if (name.equals(getString(R.string.float_window))) {
                String[] permissions6 = new String[]{
                        Manifest.permission.SYSTEM_ALERT_WINDOW,
                        Settings.ACTION_MANAGE_OVERLAY_PERMISSION};
                checkPermissionAndStart(permissions6, new PermissionCallback() {
                    @Override
                    public void doNext() {
                        SimultaneousFloatWindow simultaneousFloatWindow = new SimultaneousFloatWindow(
                                MainActivity.this,
                                getPackageName(),
                                new OnFloatClickListener() {
                                    @Override
                                    public void showType(@Nullable View view, @NonNull ShowType type) {
                                        Log.d("SimultaneousFloatWindow", "showType");
                                    }

                                    @Override
                                    public void setting(@Nullable View view) {
                                        Log.d("SimultaneousFloatWindow", "setting");
                                    }

                                    @Override
                                    public void screenRecover(@Nullable View view) {
                                        Log.d("SimultaneousFloatWindow", "screenRecover");
                                    }

                                    @Override
                                    public void closeFloatWindow(@Nullable View view) {
                                        Log.d("SimultaneousFloatWindow", "closeFloatWindow");
                                    }
                                });


                        //模拟：显示
                        simultaneousFloatWindow.changeLang("英语", "中文（简体）");
                        simultaneousFloatWindow.show();

                        //模拟：更新内容
//                        mXHandler.postDelayed(() -> {
//                            simultaneousFloatWindow.changeOriginalContent("Hello!");
//                            simultaneousFloatWindow.changeTransContent("");
//                        }, 1500);
//                        mXHandler.postDelayed(() -> {
//                            simultaneousFloatWindow.changeTransContent("你好！");
//                        }, 2000);
                    }
                });
            }
        }

    }

    private void checkPermissionAndStart(String[] permissions,PermissionCallback callback) {
        permissionUtil.checkIsHavePermission(this, permissions, callback);
    }

}